﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system.FailureServiceReference;
using System.Windows.Data;
using bdnt_pets_system.Tools;
using System.Collections.ObjectModel;

namespace bdnt_pets_system.PageViews.FailuresTraking
{
    public partial class DescrControl : BaseUserControl
    {
        FailureServiceReference.FailureServiceClient faiClient;
        public DescrControl()
        {
            InitializeComponent();
            faiClient = new FailureServiceClient();
            InitEvent();
        }

        public Int32 CuTrakingId = 0;
        //Tbl_FailureTrackingDescr CuTbl_FailureTrackingDescr
        //{
        //    get { return this.DataContext as Tbl_FailureTrackingDescr; }
        //    set { this.DataContext = value; }
        //}

        void InitEvent()
        {
            btnEdit2.Click += new RoutedEventHandler(btnEdit2_Click);
            btnEdit3.Click += new RoutedEventHandler(btnEdit2_Click);
            btnEdit4.Click += new RoutedEventHandler(btnEdit2_Click);
            btnEdit5.Click += new RoutedEventHandler(btnEdit2_Click);
            btnEdit6.Click += new RoutedEventHandler(btnEdit2_Click);
            btnEdit7.Click += new RoutedEventHandler(btnEdit2_Click);
            btnEdit8.Click += new RoutedEventHandler(btnEdit2_Click);

            btnSave2.Click += new RoutedEventHandler(btnSave2_Click);
            btnSave3.Click += new RoutedEventHandler(btnSave2_Click);
            btnSave4.Click += new RoutedEventHandler(btnSave2_Click);
            btnSave5.Click += new RoutedEventHandler(btnSave2_Click);
            btnSave6.Click += new RoutedEventHandler(btnSave2_Click);
            btnSave7.Click += new RoutedEventHandler(btnSave2_Click);
            btnSave8.Click += new RoutedEventHandler(btnSave2_Click);
            faiClient.SaveTrakingDescrCompleted += new EventHandler<SaveTrakingDescrCompletedEventArgs>(faiClient_SaveTrakingDescrCompleted);

            InitData();
        }

        void faiClient_SaveTrakingDescrCompleted(object sender, SaveTrakingDescrCompletedEventArgs e)
        {
            if (e.Result == null)
                return;
            Grid cuGd = e.UserState as Grid;
            BindOneGrid(cuGd, e.Result);

            //var desType = Int32.Parse(cuGd.DataContext.ToString());
            //var res = e.Result.Where(p => p.DesContentType == desType);
            //gdContents2.RowDefinitions.Clear();
            //int rowIndex = 0;
            //foreach (var item in res)
            //{
            //    gdContents2.RowDefinitions.Add(new RowDefinition() { MinHeight = 30 });
            //    Border bd1 = new Border();
            //    TextBlock tb1 = new TextBlock() { Margin = new Thickness(5), Text = item.DesContent };
            //    bd1.Child = tb1;
            //    Grid.SetColumn(bd1, 0);
            //    Grid.SetRow(bd1, rowIndex);
            //    cuGd.Children.Add(bd1);

            //    Border bd2 = new Border();
            //    TextBlock tb2 = new TextBlock()
            //    {
            //        Margin = new Thickness(5),
            //        Text = item.DesTime.Value.ToString("yyyy-MM-hh HH:mm:ss"),
            //        VerticalAlignment = System.Windows.VerticalAlignment.Center
            //    };
            //    bd2.Child = tb2;
            //    Grid.SetColumn(bd2, 1);
            //    Grid.SetRow(bd2, rowIndex);
            //    cuGd.Children.Add(bd2);
            //    rowIndex++;
            //}
            //var cuTb = cuGd.Tag as TextBox;
            //cuTb.DataContext = new Tbl_FailureTrackingDescr() { DesUserId = SessionManager.CurrentUser.Id, TrakingId = CuTrakingId, DesContentType = desType };

            var cuTb = cuGd.Tag as TextBox;
            var editBtn = (cuTb.Tag as Button);
            cuTb.IsEnabled = false;
            editBtn.Visibility = System.Windows.Visibility.Visible;

            //if (e.Result != 0)
            //{
            //    CuTbl_FailureTrackingDescr.Id = e.Result;
            //    var cuTb = (e.UserState as TextBox);
            //    var cuDtb = (cuTb.Tag as TextBlock);
            //    var editBtn = (cuDtb.Tag as Button);
            //    cuTb.IsEnabled = false;
            //    editBtn.Visibility = System.Windows.Visibility.Visible;
            //}
        }


        void BindOneGrid(Grid cuGd, ObservableCollection<Tbl_FailureTrackingDescr> resV)
        {
            cuGd.RowDefinitions.Clear();
            cuGd.Children.Clear();
            var desType = Int32.Parse(cuGd.DataContext.ToString());
            var res = resV.Where(p => p.DesContentType == desType);
            int rowIndex = 0;
            foreach (var item in res)
            {
                cuGd.RowDefinitions.Add(new RowDefinition());
                Border bd1 = new Border() { BorderThickness = new Thickness(1, 0, 1, 1) };
                TextBlock tb1 = new TextBlock() { Margin = new Thickness(5), Text = item.DesContent };
                bd1.Child = tb1;
                Grid.SetColumn(bd1, 0);
                Grid.SetRow(bd1, rowIndex);
                cuGd.Children.Add(bd1);

                Border bd2 = new Border() { BorderThickness = new Thickness(0, 0, 1, 1) };
                TextBlock tb2 = new TextBlock()
                {
                    Margin = new Thickness(5),
                    Text = item.DesTime.Value.ToString("yyyy-MM-hh HH:mm:ss"),
                    VerticalAlignment = System.Windows.VerticalAlignment.Center
                };
                bd2.Child = tb2;
                Grid.SetColumn(bd2, 1);
                Grid.SetRow(bd2, rowIndex);
                cuGd.Children.Add(bd2);
                rowIndex++;
            }
            var cuTb = cuGd.Tag as TextBox;
            cuTb.DataContext = new Tbl_FailureTrackingDescr() { DesUserId = SessionManager.CurrentUser.Id, TrakingId = CuTrakingId, DesContentType = desType };

        }

        void InitData()
        {
            var uId = SessionManager.CurrentUser.Id;
            btnEdit2.Tag = btnSave2;
            btnSave2.Tag = gdContents2;
            gdContents2.Tag = tbContent2;
            gdContents2.DataContext = 2;
            tbContent2.Tag = btnEdit2;
            tbContent2.SetBinding(TextBox.TextProperty, new Binding("DesContent") { Mode = BindingMode.TwoWay });
            tbContent2.DataContext = new Tbl_FailureTrackingDescr() { DesUserId = uId, TrakingId = CuTrakingId, DesContentType = 2 };

            btnEdit3.Tag = btnSave3;
            btnSave3.Tag = gdContents3;
            gdContents3.Tag = tbContent3;
            gdContents3.DataContext = 3;
            tbContent3.Tag = btnEdit3;
            tbContent3.SetBinding(TextBox.TextProperty, new Binding("DesContent") { Mode = BindingMode.TwoWay });
            tbContent3.DataContext = new Tbl_FailureTrackingDescr() { DesUserId = uId, TrakingId = CuTrakingId, DesContentType = 3 };

            btnEdit4.Tag = btnSave4;
            btnSave4.Tag = gdContents4;
            gdContents4.Tag = tbContent4;
            gdContents4.DataContext = 4;
            tbContent4.Tag = btnEdit4;
            tbContent4.SetBinding(TextBox.TextProperty, new Binding("DesContent") { Mode = BindingMode.TwoWay });
            tbContent4.DataContext = new Tbl_FailureTrackingDescr() { DesUserId = uId, TrakingId = CuTrakingId, DesContentType = 4 };

            btnEdit5.Tag = btnSave5;
            btnSave5.Tag = gdContents5;
            gdContents5.Tag = tbContent5;
            gdContents5.DataContext = 5;
            tbContent5.Tag = btnEdit5;
            tbContent5.SetBinding(TextBox.TextProperty, new Binding("DesContent") { Mode = BindingMode.TwoWay });
            tbContent5.DataContext = new Tbl_FailureTrackingDescr() { DesUserId = uId, TrakingId = CuTrakingId, DesContentType = 5 };

            btnEdit6.Tag = btnSave6;
            btnSave6.Tag = gdContents6;
            gdContents6.Tag = tbContent6;
            gdContents6.DataContext = 6;
            tbContent6.Tag = btnEdit6;
            tbContent6.SetBinding(TextBox.TextProperty, new Binding("DesContent") { Mode = BindingMode.TwoWay });
            tbContent6.DataContext = new Tbl_FailureTrackingDescr() { DesUserId = uId, TrakingId = CuTrakingId, DesContentType = 6 };

            btnEdit7.Tag = btnSave7;
            btnSave7.Tag = gdContents7;
            gdContents7.Tag = tbContent7;
            gdContents7.DataContext = 7;
            tbContent7.Tag = btnEdit7;
            tbContent7.SetBinding(TextBox.TextProperty, new Binding("DesContent") { Mode = BindingMode.TwoWay });
            tbContent7.DataContext = new Tbl_FailureTrackingDescr() { DesUserId = uId, TrakingId = CuTrakingId, DesContentType = 7 };

            btnEdit8.Tag = btnSave8;
            btnSave8.Tag = gdContents8;
            gdContents8.Tag = tbContent8;
            gdContents8.DataContext = 8;
            tbContent8.Tag = btnEdit8;
            tbContent8.SetBinding(TextBox.TextProperty, new Binding("DesContent") { Mode = BindingMode.TwoWay });
            tbContent8.DataContext = new Tbl_FailureTrackingDescr() { DesUserId = uId, TrakingId = CuTrakingId, DesContentType = 8 };
        }

        void btnSave2_Click(object sender, RoutedEventArgs e)
        {
            var saveBtn = sender as Button;
            var cuGd = (saveBtn.Tag as Grid);
            var cuTb = (cuGd.Tag as TextBox);
            var cuo = cuTb.DataContext as Tbl_FailureTrackingDescr;
            cuo.DesTime = DateTime.Now;
            faiClient.SaveTrakingDescrAsync(cuo, cuGd);
        }

        void btnEdit2_Click(object sender, RoutedEventArgs e)
        {
            var editBtn = (sender as Button);
            var saveBtn = (editBtn.Tag as Button);
            var cuGd = (saveBtn.Tag as Grid);
            var cuTb = (cuGd.Tag as TextBox);
            cuTb.IsEnabled = true;
            editBtn.Visibility = System.Windows.Visibility.Collapsed;
        }

        public void BindData(ObservableCollection<Tbl_FailureTrackingDescr> cuObjs)
        {
            BindOneGrid(gdContents2, cuObjs);
            BindOneGrid(gdContents3, cuObjs);
            BindOneGrid(gdContents4, cuObjs);
            BindOneGrid(gdContents5, cuObjs);
            BindOneGrid(gdContents6, cuObjs);
            BindOneGrid(gdContents7, cuObjs);
            BindOneGrid(gdContents8, cuObjs);
        }
    }
}
